package com.iot.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.iot.model.dto.UserInfoDto;
import com.iot.model.query.ShareQuery;
import com.iot.model.dto.ShareDto;
import com.iot.model.vo.AuthInfoVo;
import com.iot.model.vo.ShareStatusVo;
import com.iot.service.ShareService;
import com.iot.commons.ResponseResult;
import com.iot.service.UserInfoService;
import com.iot.utils.ResponseResultUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @author: dh
 * @date: 2024-04-22 14:51:44
 */
@RestController
@RequestMapping("/share")
@RequiredArgsConstructor
public class ShareController {

    private final ShareService shareService;

    private final UserInfoService userInfoService;

    /**
     * 修改分享开关状态
     * @return resultList
     */
    @PostMapping(value = "/editStatus")
    public ResponseResult editStatus(@RequestBody UserInfoDto dto) {
        Integer isOn = dto.getIsOn();
        if (null == isOn) {
            return ResponseResultUtil.paramFailure();
        }
        AuthInfoVo authInfoEntity = (AuthInfoVo) StpUtil.getSession().get("loginInfo");
        if (null == authInfoEntity) {
            return ResponseResultUtil.authInfoFailure();
        }
        dto.setAuthId(authInfoEntity.getId());
        userInfoService.editByAuthId(dto);
        return ResponseResultUtil.success();
    }

    /**
     * 查询分享开关状态
     * @return resultList
     */
    @PostMapping(value = "/getStatus")
    public ResponseResult getStatus() {
        AuthInfoVo authInfoEntity = (AuthInfoVo) StpUtil.getSession().get("loginInfo");
        if (null == authInfoEntity) {
            return ResponseResultUtil.authInfoFailure();
        }
        Integer isOn = userInfoService.getShareStatus(authInfoEntity.getId());
        if (null == isOn) {
            return ResponseResultUtil.authInfoFailure();
        }
        ShareStatusVo vo = new ShareStatusVo();
        vo.setIsOn(isOn);
        return ResponseResultUtil.success(vo);
    }

    /**
     * 分页条件搜索实现
     * @param share
     * @return resultList
     */
    @PostMapping(value = "/getList")
    public ResponseResult getList(@RequestBody ShareQuery share) {
        return  shareService.getList(share);
    }

    /**
     * 回复邀请
     * @param share
     * @return
     */
    @PostMapping(value = "/reply")
    public ResponseResult reply(@RequestBody ShareDto share) {
        return shareService.reply(share);
    }

    /**
     * 邀请关注
     * @param share
     * @return
     */
    @PostMapping(value = "/apply")
    public ResponseResult apply(@RequestBody ShareDto share) {
        return shareService.invite(share);
    }


    /**
     * 申请关注
     * @param share
     * @return
     */
    @PostMapping(value = "/invite")
    public ResponseResult invite(@RequestBody ShareDto share) {
        return shareService.apply(share);
    }

    /**
     * 申请关注
     * @param share
     * @return
     */
    @PostMapping(value = "/edit")
    public ResponseResult edit(@RequestBody ShareDto share) {
        return shareService.edit(share);
    }
}
